﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;
using System.Threading;
using System.IO;

namespace Waker
{
    public partial class FormLEDInDoorDisplayNew : Base.FormBase
    {
        public FormLEDInDoorDisplayNew()
        {
            InitializeComponent();
        }


        Dictionary<string, int> _DicSubNameAndCount = new Dictionary<string, int>();
        List<Label> _ListEventSubLabel = new List<Label>();
        private void FormLEDInDoorDisplayNew_Load(object sender, EventArgs e)
        {
            try
            {
                Screen screenPrev = null;
                if (Consts.LEDInDoorScreenIndex < 1)
                    screenPrev = Screen.AllScreens[0];
                else
                    screenPrev = Screen.AllScreens[Consts.LEDInDoorScreenIndex - 1];
                var screen = Screen.AllScreens[Consts.LEDInDoorScreenIndex];
                //this.Location = new Point(screenPrev.WorkingArea.Width, screenPrev.WorkingArea.Height);
                this.Location = screen.WorkingArea.Location;
                this.Size = new System.Drawing.Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
                lblCompanyName.Text = Session._CommanyName;
                plSubContent.Controls.Clear();
                var dt = new BAL.BAL_Cards().GetAllDepartment();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        var lbl = new Label();
                        lbl.AutoSize = lblEmpName.AutoSize;
                        lbl.TextAlign = lblEmpName.TextAlign;
                        lbl.Height = lblEmpName.Height;
                        lbl.BorderStyle = lblEmpName.BorderStyle;
                        lbl.Font = lblEmpName.Font;
                        lbl.Dock = lblEmpName.Dock;
                        lbl.Text = string.Format("{0}:0次", dr["Dept_Name"]);
                        lbl.Tag = dr["Dept_Name"].ToString();
                        _DicSubNameAndCount.Add(dr["Dept_Name"].ToString(), 0);
                        _ListEventSubLabel.Add(lbl);
                        plSubContent.Controls.Add(lbl);
                    }
                }
                this.BringToFront();
                timerCurrTime.Enabled = true;
                timerEvent.Enabled = true;
            }
            catch (Exception ex)
            {
                new Loger().LogException(ex);
            }
            finally
            {
                //if (_CallBack != null)
                //    _CallBack();
            }
        }
        public string _CurrEmpNo = string.Empty;
        private void timerEvent_Tick(object sender, EventArgs e)
        {
            try
            {
                if (Session.ListADSEventDataToDisplay == null || Session.ListADSEventDataToDisplay.Count < 1)
                    return;
                var enDisplay = Session.ListADSEventDataToDisplay[Session.ListADSEventDataToDisplay.Count - 1];
                if (enDisplay == null || enDisplay.Emp_NO == _CurrEmpNo)
                    return;
                picEvent.Image = Session.EventImage;

                lblSubName.Text = string.Format("分包商:{0}", enDisplay.Dept_Name);
                lblEmpNo.Text = string.Format("工号:{0}", enDisplay.Emp_NO);
                lblEmpName.Text = string.Format("姓名:{0}", enDisplay.EmplyeeName);
                lblWorkType.Text = string.Format("工种:{0}", enDisplay.WorkTypeName);
                lblJob.Text = string.Format("职位:{0}", enDisplay.Job);
                if (enDisplay.Emp_Photo != null && enDisplay.Emp_Photo.Length > 0)
                {
                    using (MemoryStream ms = new MemoryStream(enDisplay.Emp_Photo))
                    {
                        picIDCardPhoto.Image = Image.FromStream(ms);
                        ms.Close();
                    }
                }
                _CurrEmpNo = enDisplay.Emp_NO;
                if (_DicSubNameAndCount.Keys.Contains(enDisplay.Dept_Name))
                {
                    _DicSubNameAndCount[enDisplay.Dept_Name] += 1;
                    var lbl = _ListEventSubLabel.Where(l => l.Tag != null && l.Tag.ToString() == enDisplay.Dept_Name).FirstOrDefault();
                    if (lbl != null)
                        lbl.Text = string.Format("{0}:{1}人", enDisplay.Dept_Name, _DicSubNameAndCount[enDisplay.Dept_Name]);
                }
                _Count = 0;
            }
            catch (Exception ex)
            {
                new Loger().LogException(ex);
            }
        }

        private void ClearEventInfo()
        {
            picEvent.Image = null;
            lblSubName.Text = "分包商:";
            lblEmpNo.Text = "工号:";
            lblEmpName.Text = "姓名:";
            lblWorkType.Text = "工种:";
            lblJob.Text = "职位:";
            picIDCardPhoto.Image = null;
        }
        int _Count;
        private void timerCurrTime_Tick(object sender, EventArgs e)
        {
            lblCurrTime.Text = DateTime.Now.ToString(Consts.StrDateTimeFormat);
            if (_Count > Consts.LEDInDoorShowScend)
            {
                _Count = 0;
                ClearEventInfo();
            }
            _Count++;
        }
    }
}
